#include "imgProcManager.h"
#include "ssSysComm.h"
#include "liveMediaRead.h"
#include <sys/time.h>


ImgProcManager::ImgProcManager(std::shared_ptr<notifyCond<image_detect_info>>& nc):
    workMode(-1),initFlag(false),cond(nc)
   
{

}

ImgProcManager::~ImgProcManager(){

}

int ImgProcManager::procSysInit(){
    td_s32 s32Ret = ss_vpss_chn_cache(VIS_VPSS_GRP_ID,VIS_VPSS_ALGOR_CHN,1);
    if(s32Ret != TD_SUCCESS){
        ERRO_PRT("ss_vpss_chn_cache error\n");
        return -1;
    }

    return 0;
}

int ImgProcManager::ImgAlgorProcRun(){

    thrHandler.reset(new ThreadHelper(std::bind(&ImgProcManager::thrImgAlgorProc,this)));
    return 0;
}

int ImgProcManager::ImgAlgorProcStop(){
    if(thrHandler){
        thrHandler.reset();
    }
    
    return 0;
}


void ImgProcManager::switchDetectMode(){
    workMode = 0;
    INFO_PRT("detect mode switch to 0x00 auto\n");
}

// 要求输入为中心点坐标，画幅大小为输入帧大小
void ImgProcManager::switchTrackMode(DetectRect& rect){
    
    initRect = rect;
    kcf.reset(new KCFTracker(false,true,false,false));

    initFlag = true;
    workMode = 1;

    INFO_PRT("detect mode switch to 0x11 manual\n");
    //    fdsst.Algorithm_RunCfg(&rect); 
}


int ImgProcManager::thrImgAlgorProc(){

    if(workMode.load() == -1){
        std::this_thread::sleep_for(std::chrono::milliseconds(30));
        return 0;
    }

    //vpss chn 临时强制输出1280*1080

    ot_video_frame_info video_frame = {};
    td_s32 s32Ret = ss_vpss_chn_get_frame(VIS_VPSS_GRP_ID,VIS_VPSS_ALGOR_CHN,&video_frame,2000);
    if(s32Ret != TD_SUCCESS){
        ERRO_PRT("ss_vpss_chn_get_frame error \n");
        std::this_thread::sleep_for(std::chrono::milliseconds(30));
        return 0;
    }

    // char name[64] = {};
    // snprintf(name,64,"media/pts_%lld.jpg",video_frame.video_frame.pts);
    // cv::imwrite(name,rgbMat);

    image_detect_info rec_rect = {};

    std::vector<DetectRect> rects;

    ss_video_frame frame;
    frame.width = video_frame.video_frame.width;
    frame.height = video_frame.video_frame.height;
    frame.virt_addr[0] = (uint8_t *)video_frame.video_frame.virt_addr[0];
    frame.stride[0] = video_frame.video_frame.stride[0];

    if(workMode.load() == 0){ // 识别模式
        // cv::Mat img(video_frame.video_frame.height * 3 / 2, video_frame.video_frame.stride[0], CV_8UC1, video_frame.video_frame.virt_addr[0]);
        // cv::Mat rgbMat;
        // cv::cvtColor(img, rgbMat, cv::COLOR_YUV2BGR_YV12);
        TIMEUSE tt;
        detect.Algorithm_DoProcess(&frame,nullptr,&rects);
    }else{  // 跟踪模式
        if (initFlag == false)
        {
            //TIMEUSE tt;
            //fdsst.Algorithm_DoProcess(&frame, nullptr, &rects);
            kcf->Algorithm_DoProcess(&frame, nullptr, &rects);
        }
        else
        {
            // TIMEUSE tt;
            // fdsst.Algorithm_RunCfg(&initRect);
            initFlag = false;
            if(initRect.w()*initRect.h() <= 30*30){
               kcf->padding = 6; 
            }else{
                kcf->padding = 3; 
            }
            kcf->Algorithm_RunCfg(&initRect,&frame);
        }
    }


    //状态发送，将坐标转换到4K输入坐标系下,坐标原点为图像左上角
    constexpr double xScale = VI_INPUT_WIDTH * 1.0 / VPSS_CHN2_IMAGE_WIDTH;
	constexpr double yScale = VI_INPUT_HEIGHT * 1.0 / VPSS_CHN2_IMAGE_HEIGHT;

    for (auto rect : rects)
    {

        InfoIdentify info;
        info.x = rect.point[0][0] * xScale;
        info.y = rect.point[0][1] * yScale;

        info.w = (rect.point[3][0] - rect.point[0][0]) * xScale;
        info.h = (rect.point[3][1] - rect.point[0][1]) * yScale;

        rec_rect.detect.push_back(info);
        INFO_PRT("target pos x %d,y %d,w %d,h %d\n",info.x,info.y,info.w,info.h);
        break;
    }

    rec_rect.pts = video_frame.video_frame.pts;
    cond->conditionPub(rec_rect);


    s32Ret = ss_vpss_chn_release_frame(VIS_VPSS_GRP_ID,VIS_VPSS_ALGOR_CHN,&video_frame);
    if(s32Ret != TD_SUCCESS){
        ERRO_PRT("ss_vpss_chn_release_frame error \n");
        return -1;
    }

    return 0;
}

